package com.victor.zjbj_android.mod.login;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.victor.zjbj_android.MainActivity;
import com.victor.zjbj_android.R;
import com.victor.zjbj_android.bean.LoginConfig;
import com.victor.zjbj_android.bean.UserBean;
import com.victor.zjbj_android.mod.login.ForgetPwdActivity;
import com.victor.zjbj_android.utils.FastJsonTools;
import com.victor.zjbj_android.utils.LoginUtils;
import com.victor.zjbj_android.utils.MainUtils;


import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.victor.zjbj_android.utils.ZjbjManager;
import com.victor.zjbj_android.utils.retrofit.BaseSubscriber;
import com.victor.zjbj_android.utils.retrofit.ExceptionHandle;
import com.victor.zjbj_android.utils.retrofit.RetrofitUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity {

    private EditText et_username;
    private EditText et_password;
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        init();
        findViewById(R.id.tv_wangji).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this,ForgetPwdActivity.class));
            }
        });
    }

    protected void init() {
        if (MainUtils.isOnline(this)){
            startActivity(new Intent(LoginActivity.this,MainActivity.class));
            finish();
        }
        BarUtils.setStatusBarAlpha(LoginActivity.this,0);
        et_password=findViewById(R.id.et_password);
        et_username=findViewById(R.id.et_username);
        btn_login=findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(et_username.getText().toString().equals("")&&et_password.getText().toString().equals("")){
                    ToastUtils.showShort("用户名或密码不能为空");
                    return;
                }

//                TZSBApplication.setBaseurl(et_baseurl.getText().toString().trim());
//                RetrofitUtils.baseUrl="http://"+TZSBApplication.getBaseurl();
//                LogUtils.e("22",RetrofitUtils.baseUrl);
                //SPUtils.getInstance().put("url",et_baseurl.getText().toString());
                //LogUtils.e(SPUtils.getInstance().getString("url"));
                RetrofitUtils mRetrofit1 = RetrofitUtils.getInstance(getApplicationContext()).createBaseApi();
                LogUtils.e("11");
                final JSONObject json=new JSONObject();
                try {
                    json.put("uniqid",et_username.getText().toString().trim());
                    json.put("password",et_password.getText().toString().trim());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                LogUtils.e("222");
                ZjbjManager tzsbManager=new ZjbjManager();
                LogUtils.e(json.toString());
                if(mRetrofit1!=null){
                    LogUtils.e("aaa");
                }
                tzsbManager.userLogin(json.toString(), mRetrofit1, new BaseSubscriber() {
                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable e) {
                        LogUtils.e("bb",e.message);
                    }

                    @Override
                    public void onSuccess(JSONObject jsonObject) throws JSONException {
                        LogUtils.e("333");
                        UserBean bean= FastJsonTools.getBean(jsonObject.toString(),UserBean.class);
                        LogUtils.e("login",jsonObject.toString());
                        if(bean!=null){
                            if(bean.getCode().equals("0")){

                                LoginConfig.setLoginConfig(LoginConfig.getInstance().setAutoLogin(true).setuid(bean.getData().getUniqid())

                                        .setAppToken(bean.getData().getToken())
                                        .setSn(bean.getData().getSn())
                                        .setUsername(bean.getData().getName())
                                        .setRole(bean.getData().getRole_list_name())

                                );

                                ToastUtils.showShort("登录成功!");
                                try {
                                    LoginUtils.setLoginConfig(LoginActivity.this, LoginUtils.serialize(LoginConfig.getLoginConfig()));
                                } catch (Exception e) {
                                    Log.e("e", e.getMessage());
                                }
                                startActivity(new Intent(LoginActivity.this,MainActivity.class));
                                finish();
                            }
                            else
                            {
                                ToastUtils.showShort(bean.getInfo().toString());
                            }
                        }

                    }
                });
            }
        });

    }

}
